%% Author: zhangjun
%% Created: 2008-11-23
%% Description: TODO: Add description to erltest1
-module(erltest1).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([init/1]).
-export([main/1]).
-export([start/1]).

-behaviour(gen_server).%like java's implement,you must implement this export interface

%%
%% API Functions
%%

%%
%% TODO: Add description of init/function_arity
%%
start(Name) ->
    try register(Name,spawn(fun() -> loop([]) end)) 
    catch 
        _:_ -> false
    end.

loop(List) ->
    receive
		Any ->
            io:format("Receive ~p~n", [Any]),
            loop(List)
	end.

init(B) -> 
	io:format("Hello World! input = ~w is a list?: ~w list is ~w atom is ~w~n", [B,is_list(B),head(B),atom_to_list(head(B))]),
    Sb = list_to_integer(atom_to_list(head(B))),
    io:format("input is ~w~n", [Sb]),
    init:stop().

head([Head|Rest]) ->
    Head.

main([A]) ->
    I = list_to_integer(atom_to_list(A)),
    F = fac(I),
	io:format("factorial ~w = ~w~n", [I,F]),
	init:stop().

%%
%% Local Functions
%%
fac(0) -> 1;
fac(N) -> N*fac(N-1).
